<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/1/4 0004
 * Time: 下午 7:33
 */
namespace app\api\controller;

use think\Request;

class Stock extends Base{
    public static function getStock($product_id){
        $stock_num = 0;
        $stock = \app\admin\model\Stock::where('product_id',$product_id)->find();
        if($stock){
            $stock_num = $stock->marketable;
        }
        return $stock_num;
    }

    //单个商品单独检测库存
    public function checkStock($product_id, $count){
        if(Request::instance()->isPost()){
            if(!empty($product_id) && !empty($count)){
                $Stock = \app\admin\model\Stock::where('product_id',$product_id)->lock(true)->field('marketable')->find();
                if($Stock){
                    if($Stock['marketable'] >= $count){
                        return show(config('status.SUCCESS_STATUS'),'ok',$Stock['marketable']);
                    }else{
                        return show(config('status.ERROR_STATUS'),'库存不足','库存不足');
                    }
                }
            }
            return show(config('status.ERROR_STATUS'),'参数错误','参数错误');
        }else{
            return show(config('status.ERROR_STATUS'),'非法请求','非法请求');
        }

    }
}